﻿Imports BUS
Imports DAO
Imports DTO
Public Class FormLapHoaDonBanSach

#Region "Biến toàn cục"
    Private makh As Integer = 0
    Private ngaylap As Date
    Private tongtien As Double = 0

    Private masach As Integer = 0
    Private slmua As Integer = 0
    Private dg As Double = 0
    Private thanhtien As Double = 0

    Private flaghople As Integer = 0
    Private slton As Integer = 0
    Private sltonsauban As Integer = 0
    Private masachdangchon As Integer = -1
    Private dssachmua As New List(Of CT_HoaDon_DTO)()

    Dim flag As Integer = 0
#End Region

#Region "Load form"
    Private Sub FormLapHoaDonBanSach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LayTen(ComboBoxTenSach)
        ButtonSua.Enabled = False
        ButtonXoa.Enabled = False
        ComboBoxTenSach.SelectedIndex = -1
        flag = 1
    End Sub
#End Region

#Region "Load Ten Sach len Combobox"

    Public Shared Sub LayTen(ByVal cbo As ComboBox)
        Dim sql As String
        sql = "Select * from Sach"
        Dim BangSach As New DataTable()
        BangSach = DataProvider.ExcuteReader(sql)
        cbo.DataSource = BangSach
        cbo.DisplayMember = "TenSach"
        cbo.ValueMember = "MaSach"
    End Sub

#End Region

#Region "Hàm clear các textbox"
    Private Sub Clear()
        ComboBoxTenSach.Text = ""
        TextBoxTacGia.Text = ""
        TextBoxSL.Text = ""
        TextBoxTheLoai.Text = ""
        TextBoxDG.Text = ""
    End Sub
#End Region

#Region "Load DataGridView"

    Private Sub LoadGridView()
        Dim stt As Integer = 1
        DataGridView1.Rows.Clear()
        tongtien = 0
        For Each s As CT_HoaDon_DTO In dssachmua
            Dim sql As String = String.Format("select * from Sach s,TheLoai tl where s.MaTheLoai = tl.MaTheLoai and MaSach = {0}", s.MaSach)
            Dim tb As DataTable = DataProvider.ExcuteReader(sql)
            DataGridView1.Rows.Add(stt.ToString(), tb.Rows(0).Item(1).ToString(), tb.Rows(0).Item(8).ToString(), tb.Rows(0).Item(3).ToString(), s.SoLuongMua, s.DonGia, s.ThanhTien)
            tongtien = tongtien + s.ThanhTien
            stt = stt + 1
        Next
        DataGridView1.ClearSelection()
        TextBoxTongTien.Text = tongtien.ToString()
    End Sub

#End Region

#Region "Hàm xử lý chọn sách từ combobox"

    Private Sub ChonSach()
        Try
            masach = ComboBoxTenSach.SelectedValue
            Dim tb As DataTable = Sach_BUS.LaySachTheoMa(masach)
            TextBoxTheLoai.Text = tb.Rows(0).Item(8).ToString()
            TextBoxTacGia.Text = tb.Rows(0).Item(3).ToString()
            TextBoxDG.Text = tb.Rows(0).Item(5).ToString()
            slton = tb.Rows(0).Item(4).ToString()
        Catch ex As Exception
            MessageBox.Show("Tên sách không hợp lệ", "Thông báo")
            Clear()
            ComboBoxTenSach.Focus()
        End Try
    End Sub


    Private Sub ComboBoxTenSach_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxTenSach.SelectedIndexChanged
        If flag = 1 Then
            ChonSach()
        End If
    End Sub

    Private Sub ComboBoxTenSach_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxTenSach.Leave
        ChonSach()
    End Sub
#End Region

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.CheckState = CheckState.Checked Then
            TextBoxDG.ReadOnly = False
        Else
            TextBoxDG.ReadOnly = True
        End If
    End Sub

#Region "Xử lý nút Xác nhận"
    Private Sub ButtonXacNhan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonXacNhan.Click
        If flaghople = 1 Then
            If masachdangchon = -1 Then
                Dim trung As Integer = 1
                For Each s As CT_HoaDon_DTO In dssachmua
                    If s.MaSach = masach Then
                        trung = 0
                    End If
                Next
                If trung = 1 Then
                    Dim sql As String = "select SL_TonSauBan from ThamSo"
                    sltonsauban = Integer.Parse(DataProvider.ExcuteScalar(sql).ToString())
                    slmua = Integer.Parse(TextBoxSL.Text)

                    If slton - slmua < sltonsauban Then
                        MessageBox.Show("Đầu sách này không còn đủ số lượng để bán", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                        TextBoxSL.Clear()
                        TextBoxSL.Focus()
                    Else
                        dg = Double.Parse(TextBoxDG.Text)
                        thanhtien = slmua * dg
                        Dim cthd As New CT_HoaDon_DTO(masach, slmua, dg, thanhtien)
                        dssachmua.Add(cthd)
                        LoadGridView()
                        Clear()
                    End If
                Else
                    MessageBox.Show("Đầu sách này đã Xác Nhận", "Thông báo")
                    Clear()
                End If
            Else
                MessageBox.Show("Đang sửa Sách", "Thông báo")
            End If
        End If
    End Sub
#End Region

#Region "Xử lý nút lập hoá đơn"
    Private Sub ButtonLap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLap.Click
        If flaghople = 1 Then
            Try
                ngaylap = DateTimePicker1.Value
                Dim hoadon As New HoaDon_DTO(makh, ngaylap, tongtien)
                HoaDon_BUS.LapHoaDon(hoadon)

                Dim sql As String = "select max(MaHoaDon) from HoaDon"
                Dim mahd As Integer = DataProvider.ExcuteScalar(sql)

                For Each cthd As CT_HoaDon_DTO In dssachmua
                    cthd.MaHD = mahd
                    HoaDon_BUS.NhapChiTietHoaDon(cthd)
                Next
                dssachmua.Clear()
                Clear()
                TextBoxCMND.Clear()
                TextBoxKH.Clear()
                TextBoxTongTien.Clear()
                flaghople = 0
                masachdangchon = -1
                MessageBox.Show("Lập hoá đơn thành công", "Thông báo")
                DataGridView1.Rows.Clear()
            Catch ex As Exception
                MessageBox.Show("Lập hoá đơn thất bại", "Thông báo")
            End Try
        End If
    End Sub
#End Region


    Private Sub ButtonThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonThoat.Click
        Me.Close()
    End Sub

#Region "Hàm xử lý nhập chuột vào từng dòng trong datagridview"
    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
        Dim index As Integer = DataGridView1.CurrentRow.Index
        Dim stt As Integer = Integer.Parse(DataGridView1.Rows(index).Cells(0).Value)
        masachdangchon = stt - 1
        ComboBoxTenSach.Text = DataGridView1.Rows(index).Cells(1).Value
        TextBoxTheLoai.Text = DataGridView1.Rows(index).Cells(2).Value
        TextBoxTacGia.Text = DataGridView1.Rows(index).Cells(3).Value
        TextBoxSL.Text = dssachmua(masachdangchon).SoLuongMua
        TextBoxDG.Text = dssachmua(masachdangchon).DonGia
        ButtonSua.Enabled = True
        ButtonXoa.Enabled = True
        ButtonXacNhan.Enabled = False
    End Sub
#End Region

#Region "Xử lý nút xoá"
    Private Sub ButtonXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonXoa.Click
        If masachdangchon > -1 Then
            dssachmua.RemoveAt(masachdangchon)
            LoadGridView()
            ButtonSua.Enabled = False
            ButtonXoa.Enabled = False
            ButtonXacNhan.Enabled = True
            masachdangchon = -1
        Else
            MessageBox.Show("Chưa chọn sách để xóa", "Thông báo")
        End If
    End Sub
#End Region

#Region "Xử lý nút sửa"
    Private Sub ButtonSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSua.Click
        If masachdangchon > -1 Then
            masach = ComboBoxTenSach.SelectedValue
            Dim sql As String = String.Format("select SoLuong from Sach where MaSach = {0}", masach)
            slton = Integer.Parse(DataProvider.ExcuteScalar(sql).ToString())
            slmua = Integer.Parse(TextBoxSL.Text)
            If slton - slmua >= sltonsauban Then
                dssachmua(masachdangchon).MaSach = ComboBoxTenSach.SelectedValue
                dssachmua(masachdangchon).SoLuongMua = TextBoxSL.Text
                dssachmua(masachdangchon).DonGia = TextBoxDG.Text
                dssachmua(masachdangchon).ThanhTien = dssachmua(masachdangchon).SoLuongMua * dssachmua(masachdangchon).DonGia
                Clear()
                LoadGridView()
                ButtonSua.Enabled = False
                ButtonXoa.Enabled = False
                ButtonXacNhan.Enabled = True
                CheckBox1.Checked = False
                masachdangchon = -1
            Else
                MessageBox.Show("Đầu sách này không còn đủ số lượng để bán", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                TextBoxSL.Clear()
                TextBoxSL.Focus()
            End If
        Else
            MessageBox.Show("Chưa chọn sách để sửa", "Thông báo")
        End If
    End Sub
#End Region

#Region "Xử lý kiểm tra CMND khi rời textboxCMND"
    Private Sub TextBoxCMND_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxCMND.Leave
        If (TextBoxCMND.Text <> "" And TextBoxCMND.Text.Length = 9 And IsNumeric(TextBoxCMND.Text) = True) Then
            Dim flag1 As Integer = KhachHang_BUS.KiemTraKhachHang(TextBoxCMND.Text)
            If flag1 = 0 Then
                flaghople = 0
                TextBoxKH.Clear()
                If MessageBox.Show("Khách hàng này không tồn tại.Có muốn đang ký hay không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    Dim f As New FormThemKhachHang()
                    f.TextBoxCMND.Text = TextBoxCMND.Text
                    f.ShowDialog()
                    TextBoxCMND.Text = f.cmnd
                End If
                TextBoxCMND.Focus()
            Else
                If flag1 = 1 Then
                    MessageBox.Show("Số tiền nợ của khách hàng này vượt quá số nợ tối đa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    flaghople = 0
                    TextBoxCMND.Focus()
                    TextBoxKH.Clear()
                Else
                    MessageBox.Show("Khách hàng hợp lệ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    flaghople = 1
                    makh = flag1
                    Dim sql As String = String.Format("select TenKH from KhachHang where Makh = {0}", makh)
                    TextBoxKH.Text = DataProvider.ExcuteScalar(sql).ToString()
                End If
            End If
        Else
            MessageBox.Show("CMND không hợp lệ", "Thông báo")
            TextBoxCMND.Focus()
        End If
    End Sub
#End Region

#Region "Xử lý nút reset"
    Private Sub ButtonReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonReset.Click
        Clear()
        ButtonSua.Enabled = False
        ButtonXoa.Enabled = False
        ButtonXacNhan.Enabled = True
        DataGridView1.ClearSelection()
    End Sub
#End Region

#Region "Đổi màu các button"
    Public Sub MauMoi(ByVal btn As Button, ByVal flag As Integer)

        If flag = 1 Then
            btn.BackColor = Color.FromArgb(CType(CType(128, Byte), Integer), CType(CType(128, Byte), Integer), CType(CType(255, Byte), Integer))
        Else
            btn.BackColor = Color.Gray
        End If
    End Sub
#End Region

#Region "Gọi hàm đổi màu cho nút sửa"
    Private Sub ButtonSua_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSua.EnabledChanged
        If ButtonSua.Enabled = True Then
            MauMoi(ButtonSua, 1)
        Else
            MauMoi(ButtonSua, 2)
        End If
    End Sub
#End Region


#Region "Gọi hàm đổi màu cho nút Xóa"
    Private Sub ButtonXoa_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonXoa.EnabledChanged
        If ButtonXoa.Enabled = True Then
            MauMoi(ButtonXoa, 1)
        Else
            MauMoi(ButtonXoa, 2)
        End If
    End Sub
#End Region


#Region "Gọi hàm đổi màu cho nút Xác nhận"

    Private Sub ButtonXacNhan_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonXacNhan.EnabledChanged
        If ButtonXacNhan.Enabled = True Then
            MauMoi(ButtonXacNhan, 1)
        Else
            MauMoi(ButtonXacNhan, 2)
        End If
    End Sub
#End Region

End Class